﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace D2D2.Core
{
    public enum ProgressType
    {
        Normal = 0,
        Blue = 1,
        Green = 2,
        Gold = 3,
        Purple = 4
    }

    public class ProgressManager
    {
        private readonly Texture2D _texture;
        private readonly Rectangle _initialLocation;
        private float _totalProgress;
        private float _currentProgress;
        private float _targetProgress;
        private float _deltaProgress;
        private float _kProgress;
        private float _decreaseVelosity = 0;
        private float _decreaseTime = 0;
        private const float TIME_FOR_HIT = 0.15f;
        private float _timeSinceLastIncrease = 0.0f;
        private bool _init;
        private float _totalHit;
        private float _remainingHit;

        public ProgressManager(Texture2D texture, Rectangle locaton)
        {      
            _texture = texture;            
            _initialLocation = locaton;
            EndDecreaseProgress = true;
        }

        public bool EndDecreaseProgress { get; private set; }

        public void SetProgress(float totalProgress)
        {
            SetProgress(totalProgress, totalProgress);
        }

        public void SetProgress(float currentProgress, float totalProgress)
        {
            _totalProgress = totalProgress;
            _currentProgress = currentProgress;

            _kProgress = _initialLocation.Width/totalProgress;
        }

        public ProgressType ProgressType { get; set; }

        private Rectangle CurrentLocation
        {
            get
            {
                Rectangle currentLocation = _initialLocation;
                currentLocation.Width = (int) (_kProgress*_currentProgress);

                return currentLocation;
            }
        }

        public void DecreaseProgress(float deltaProgress, float decreaseTime)
        {
            DecreaseProgress(deltaProgress, _totalProgress, decreaseTime);
        }

        public void DecreaseProgress(float deltaProgress, float totalProgress, float decreaseTime)
        {
            EndDecreaseProgress = false;
            _decreaseTime = decreaseTime;
            _targetProgress = MathHelper.Max(_currentProgress - deltaProgress, 0);
            _deltaProgress = deltaProgress;
            _totalProgress = totalProgress;
            _decreaseVelosity = _deltaProgress/decreaseTime;
        }

        public void Update(GameTime gameTime)
        {
            //float elapsedTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, CurrentLocation, new Rectangle(0, GetTextureY(), 100, 10), Color.White);
        }

        private int GetTextureY()
        {
            return ((int) ProgressType)*10;
        }
    }
}
